<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="配送方式" prop="shippingType">
        <el-select
          v-model="queryParams.shippingType"
          placeholder="请选择"
          clearable
        >
          <el-option label="快递配送" :value="1" />
          <el-option label="同城配送" :value="3" />
          <el-option label="自提" :value="2" />
        </el-select>
      </el-form-item> -->

      <el-form-item label="支付时间">
        <el-date-picker
          v-model="daterangePayTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择订单状态"
          clearable
          filterable
        >
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="2.5">
        <el-button type="primary" size="small" @click="handleTx"
          >申请提现</el-button
        >
      </el-col>

      <el-col :span="9">
        <div style="line-height: 32px">
          我的余额：<span style="color: red; font-size: 20px">{{
            balance
          }}</span
          >元
        </div>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="storeOrderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="订单号"
        align="center"
        prop="orderId"
        show-overflow-tooltip
      />
      <el-table-column label="用户姓名" align="center" prop="realName" />
      <el-table-column
        label="用户电话"
        align="center"
        prop="userPhone"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="详细地址"
        align="center"
        prop="userAddress"
        show-overflow-tooltip
      /> -->
      <!-- <el-table-column label="运费" align="center" prop="freightPrice" />
      <el-table-column label="商品数" align="center" prop="totalNum" /> -->
      <el-table-column label="订单总价" align="center" prop="totalPrice" />
      <el-table-column label="支付金额" align="center" prop="payPrice" />
      <!-- <el-table-column label="优惠券金额" align="center" prop="couponPrice" /> -->
      <el-table-column label="配送方式" align="center" prop="shippingType">
        <template slot-scope="scope">
          <span v-if="scope.row.shippingType == 1">快递配送</span>
          <span v-if="scope.row.shippingType == 2">自提</span>
          <span v-if="scope.row.shippingType == 3">同城配送</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="payTypeName" label="支付状态" />
      <el-table-column prop="statusName" label="订单状态" /> -->
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="订单状态" align="center" prop="paid">
        <template slot-scope="scope">
          <span v-if="scope.row.paid == 0">未支付</span>
          <span v-if="scope.row.paid == 1">已支付</span>
        </template>
      </el-table-column> -->
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">待支付</span>
          <span v-else-if="scope.row.refundStatus == -3">已退款</span>
          <span v-else-if="scope.row.status == 1 && scope.row.shippingType == 1"
            >待发货</span
          >
          <span v-else-if="scope.row.status == 1 && scope.row.shippingType == 2"
            >待核销</span
          >
          <span v-else-if="scope.row.status == 2 && scope.row.shippingType == 1"
            >待收货</span
          >
          <span
            v-else-if="
              scope.row.status == 2 &&
              scope.row.shippingType == 3 &&
              scope.row.senderStatus == 0
            "
            >待取货</span
          >
          <span
            v-else-if="
              scope.row.status == 2 &&
              scope.row.shippingType == 3 &&
              scope.row.senderStatus == 1
            "
            >待送达</span
          >
          <span
            v-else-if="
              scope.row.status == 2 &&
              scope.row.shippingType == 3 &&
              scope.row.senderStatus == 2
            "
            >已完成</span
          >
          <span v-else-if="scope.row.status == 3">已完成</span>

          <!-- <dict-tag
            :options="dict.type.order_status"
            :value="scope.row.status"
          /> -->
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="退款备注"
        align="center"
        prop="refundReason"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="操作"
        align="center"
        width="240"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="handleDetail(scope.row)"
            >订单详情</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:storeOrder:edit']"
            v-if="
              scope.row.status === 1 &&
              scope.row.shippingType == 1 &&
              scope.row.refundStatus != -3
            "
            >发货</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="handleHx(scope.row)"
            v-hasPermi="['business:storeOrder:edit']"
            v-if="scope.row.status === 1 && scope.row.shippingType == 2"
            >核销</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="handleTx(scope.row)"
            v-hasPermi="['business:storeOrder:edit']"
            v-if="scope.row.status != 0 && scope.row.refundStatus != -3"
            >退款</el-button
          >
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:storeOrder:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item
          v-if="form.shippingType == 1"
          label="快递公司"
          prop="deliveryName"
        >
          <el-select
            v-model="form.deliveryName"
            filterable
            placeholder="请选择"
            style="width: 370px"
          >
            <el-option
              v-for="item in expressList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.shippingType == 1"
          label="快递单号"
          prop="deliveryId"
        >
          <el-input v-model="form.deliveryId" style="width: 370px" />
        </el-form-item>
        <el-form-item
          v-if="form.shippingType == 3"
          label="配送员"
          prop="senderId"
        >
          <el-select
            v-model="form.senderId"
            filterable
            placeholder="请选择"
            style="width: 370px"
          >
            <el-option
              v-for="item in deliveryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="核销" :visible.sync="openHx" width="500px" append-to-body>
      <el-form ref="formHx" :model="formHx" :rules="rulesHx" label-width="80px">
        <el-form-item label="核销码" prop="code">
          <el-input v-model="formHx.code" style="width: 370px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHx">确 定</el-button>
        <el-button @click="openHx = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="退款" :visible.sync="openTx" width="500px" append-to-body>
      <el-form ref="formTx" :model="formTx" :rules="rulesTx" label-width="80px">
        <el-form-item label="退款备注" prop="refundReason">
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 10 }"
            placeholder="请输入退款备注"
            v-model="formTx.refundReason"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTx">确 定</el-button>
        <el-button @click="openTx = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStoreOrder,
  getStoreOrder,
  delStoreOrder,
  addStoreOrder,
  updateStoreOrder,
  sendStoreOrder,
  hxStoreOrder,
  getBalanceApi,
  refundStoreOrder,
} from "@/api/business/storeOrder";
import { listAllExpress } from "@/api/business/express";
import { listAllDeliveryman } from "@/api/business/deliveryman";
export default {
  name: "StoreOrder",
  dicts: ["order_status", "pay_status"],
  data() {
    return {
      balance: "",
      openTx: false,
      formTx: {
        extractPrice: "",
        type: 1,
        bankAddress: "",
        bankCode: "",
      },
      rulesTx: {
        bankAddress: [
          {
            required: true,
            message: "请填写银行卡户名",
            trigger: "blur",
          },
        ],
        bankCode: [
          {
            required: true,
            message: "请填写银行卡卡号",
            trigger: "blur",
          },
        ],
        extractPrice: [
          {
            required: true,
            message: "请填写提现金额",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择提现方式",
            trigger: "change",
          },
        ],
      },
      openHx: false,
      formHx: {
        id: "",
        code: "",
      },
      rulesHx: {
        code: [
          {
            required: true,
            message: "请填写核销码",
            trigger: "blur",
          },
        ],
      },
      deliveryList: [],
      expressList: [],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      storeOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 删除标志时间范围
      daterangePayTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        extendOrderId: null,
        uid: null,
        realName: null,
        userPhone: null,
        userAddress: null,
        cartId: null,
        freightPrice: null,
        totalNum: null,
        totalPrice: null,
        totalPostage: null,
        payPrice: null,
        payPostage: null,
        deductionPrice: null,
        paid: null,
        payTime: null,
        status: null,
        refundPrice: null,
        deliverySn: null,
        deliveryName: null,
        deliveryType: null,
        deliveryId: null,
        shippingType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deliveryName: [
          {
            required: true,
            message: "请选择快递公司",
            trigger: "change",
          },
        ],
        deliveryId: [
          {
            required: true,
            message: "请填写快递单号",
            trigger: "blur",
          },
        ],
        senderId: [
          {
            required: true,
            message: "请选择配送员",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getExpressList();
    this.getdeliveryList();
    // this.getBalance();
  },
  methods: {
    handleTx(row) {
      this.formTx = {
        id: row.id,
        refundReason: "",
      };
      this.openTx = true;
    },
    submitTx() {
      this.$refs["formTx"].validate((valid) => {
        if (valid) {
          refundStoreOrder(this.formTx).then((res) => {
            this.$modal.msgSuccess("退款成功");
            this.openTx = false;
            this.getList();
          });
        }
      });
    },
    getBalance() {
      getBalanceApi().then((res) => {
        console.log(res);
        this.balance = res.data.balance;
      });
    },
    handleHx(row) {
      this.formHx = {
        id: row.id,
        code: "",
      };
      this.openHx = true;
    },
    //快递公司
    getExpressList() {
      listAllExpress().then((res) => {
        this.expressList = res.data;
      });
    },
    //配送员
    getdeliveryList() {
      listAllDeliveryman().then((res) => {
        this.deliveryList = res.data;
      });
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePayTime && "" != this.daterangePayTime) {
        this.queryParams.params["beginPayTime"] = this.daterangePayTime[0];
        this.queryParams.params["endPayTime"] = this.daterangePayTime[1];
      }
      listStoreOrder(this.queryParams).then((response) => {
        this.storeOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        deliveryName: "",
        deliveryId: "",
        senderId: null,
        shippingType: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePayTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getStoreOrder(id).then((response) => {
        this.form.orderId = response.data.orderId;
        this.form.deliveryName = response.data.deliveryName;
        this.form.deliveryId = response.data.deliveryId;
        this.form.senderId = response.data.senderId;
        this.form.shippingType = response.data.shippingType;
        this.open = true;
        this.title = "发货";
      });
    },
    handleDetail(row) {
      this.$router.push({ path: "/order/detail/" + row.id });
    },
    submitHx() {
      this.$refs["formHx"].validate((valid) => {
        if (valid) {
          hxStoreOrder(this.formHx).then((response) => {
            this.$modal.msgSuccess("核销成功");
            this.openHx = false;
            this.getList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          sendStoreOrder(this.form).then((response) => {
            this.$modal.msgSuccess("发货成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delStoreOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/storeOrder/export",
        {
          ...this.queryParams,
        },
        `storeOrder_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
